package com.bsoft.hospitalchs.ui.subscribe;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bsoft.hospitalchs.R;
import com.bsoft.hospitalchs.base.BaseActivity;
import com.bsoft.hospitalchs.utils.SpUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 泅渡者
 * Created on 2017/6/22.
 */

public class SubscribeActivity  extends BaseActivity<SubscribeContract.Presenter> implements SubscribeContract.View{

    @Bind(R.id.text_Loading)
    TextView textLoading;
    @Bind(R.id.webview)
    WebView webview;
    @Bind(R.id.ll_back)
    LinearLayout llBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    WebSettings mWebSettings;

    private int Client;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscrice);
        ButterKnife.bind(this);
        this.createPresenter(new SubscribePresenter(this, this));
        initview();
    }

    private void initview() {
        Client = getIntent().getIntExtra("Client",0);
        tvTitle.setText("预约记录");

        mWebSettings = webview.getSettings();
        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);//取消滚动条


        //设置不用系统浏览器打开,直接显示在当前Webview
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                view.loadUrl("file:///android_asset/error.html");
            }
        });

        //设置WebChromeClient类
        webview.setWebChromeClient(new WebChromeClient() {
            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {

            }


            //获取加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress < 100) {
                    String progress = "正在加载：" + newProgress + "%";
                    textLoading.setText(progress);
                } else if (newProgress == 100) {
                    String progress = newProgress + "%";
                    textLoading.setText(progress);
                    textLoading.setVisibility(View.GONE);
                    webview.setVisibility(View.VISIBLE);
                }
            }
        });


        //设置自适应屏幕，两者合用
        mWebSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        mWebSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        //缩放操作
        mWebSettings.setSupportZoom(false); //支持缩放，默认为true。是下面那个的前提。
        mWebSettings.setBuiltInZoomControls(false); //设置内置的缩放控件。若为false，则该WebView不可缩放
        mWebSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

        //其他细节操作
        mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebSettings.setAllowFileAccess(true); //设置可以访问文件
        mWebSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        mWebSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

        String YiIP = (String) SpUtils.get(SubscribeActivity.this, "YiIP", "219.234.5.196:8066");

        webview.loadUrl("http://"+ YiIP+"/ywk/appointmentManager/gotoOrderHtml?idOrganization=330102356"+"");
    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showLoadingDialog() {

    }
    @OnClick({R.id.ll_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
        }
    }
}
